package pers.cj.pdos.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.constant.OrderType;
import pers.cj.pdos.controller.base.BaseController;

import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.House;
import pers.cj.pdos.entity.OrderRule;
import pers.cj.pdos.entity.Store;
import pers.cj.pdos.service.IHouseService;
import pers.cj.pdos.service.IOrderRuleService;
import pers.cj.pdos.service.IStoreService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.HouseVo;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/organization")
public class HouseController extends BaseController {
    @Autowired
    private IHouseService houseService;

    @RequiresPermissions("house:query")
    @RequestMapping("/house")
    public String house() {
        return "org/house";
    }

    @RequiresPermissions("house:insert")
    @RequestMapping("/houseNew")
    public String houseNew() {
        return "org/houseNew";
    }

    @RequiresPermissions("house:insert")
    @RequestMapping("/houseEdit")
    public String houseEdit(Model model, String id) {
        model.addAttribute("id",id);
        return "org/houseEdit";
    }

    @ResponseBody
    @RequiresPermissions("house:query")
    @RequestMapping("/house/getHouses")
    public ResponseList getHouses(int page, int limit, String searchStoreId, String searchContent) {
        Company company = getCompany();

        List<HouseVo> list = houseService.listVoPage(page, limit, company.getId(), PdosUtils.getViewHouseStoreId(getLoginUserVo(), searchStoreId), searchContent, null);
        return ResponseList.success(houseService.count(company.getId(),searchStoreId, searchContent,null), list);
    }

    @ResponseBody
    @RequestMapping("/house/getNormList")
    public ResponseList getNormList() {
        Company company = getCompany();
        List<HouseVo> list = houseService.listNormalVo( company.getId(), PdosUtils.getViewHouseStoreId(getLoginUserVo(), null));
        return ResponseList.success(list.size(), list);
    }

    @ResponseBody
    @RequiresPermissions("house:insert")
    @RequestMapping("/house/add")
    public ResponseEntity add(@RequestBody House house) {
        if (Utils.isEmpty(house.getNo())) {
            return ResponseEntity.fail("编号不能为空，请点击重新获取");
        }

        Company company = getCompany();
        if (houseService.checkName(house.getName(),company.getId())){
            return ResponseEntity.fail("仓库名称已存在");
        }

        if (houseService.checkNo(house.getNo(),company.getId())){
            return ResponseEntity.fail("仓库编号已存在");
        }

        DataUtil.processEntity(company,house);
        houseService.insert(house);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("house:insert")
    @RequestMapping("/house/update")
    public ResponseData update(@RequestBody House house) {
        Company company = getCompany();
        House temp = houseService.getById(house.getId());
        if (temp==null){
            ResponseData.fail("id不正确");
        }
        //与原名不同的名称再检查是否存在
        if (!temp.getName().equals(house.getName()) && houseService.checkName(house.getName(),company.getId())){
            return ResponseData.fail("仓库名称已存在");
        }

        //与原单号不同的单号再检查是否存在
        if (!temp.getNo().equals(house.getNo()) && houseService.checkNo(house.getNo(),company.getId())){
            return ResponseData.fail("仓库编号已存在");
        }

        if (house.getStatus()!=1 && houseService.check(house.getId())) {
            return ResponseData.fail("已被使用，无法停用");
        }

        DataUtil.processEntity(false,company,house);
        houseService.update(house);

        return ResponseData.success(houseService.getVoById(house.getId()));
    }

    @ResponseBody
    @RequestMapping("/house/lock")
    public ResponseEntity lock(String id) {
        if (Utils.isEmpty(id)) {
            return ResponseEntity.fail("id不能为空");
        }
        House house = new House();
        house.setId(id);
        house.setLocking(true);
        houseService.updateById(house);

        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/house/unlock")
    public ResponseEntity unlock(String id) {
        if (Utils.isEmpty(id)) {
            return ResponseEntity.fail("id不能为空");
        }
        House house = new House();
        house.setId(id);
        house.setLocking(false);
        houseService.updateById(house);

        return ResponseEntity.success();
    }


    @ResponseBody
    @RequiresPermissions("house:delete")
    @RequestMapping("/house/delete")
    public ResponseEntity delete(@RequestBody House house) {
        House old = houseService.getById(house.getId());
        if (old == null) {
            return ResponseEntity.fail("传入的id有误");
        }

        if (old.getSys()) {
            return ResponseEntity.fail("系统仓库，无法删除");
        }

        if (houseService.check(house.getId())) {
            return ResponseEntity.fail("已被使用，无法删除");
        }

        houseService.remove(house);
        return ResponseEntity.success();

    }

    @ResponseBody
    @RequestMapping("/house/getHouseByStore")
    public ResponseData getHouseByStore(String storeId) {
        List<House> houseList = houseService.listNormalByStore(storeId);
        return ResponseData.success(houseList);
    }

    @ResponseBody
    @RequestMapping("/house/listNormalVo")
    public ResponseData listNormalVo() {
        List<HouseVo> houseVoList = houseService.listNormalVo(getCompany().getId(), PdosUtils.getViewHouseStoreId(getLoginUserVo(), null));
        return ResponseData.success(houseVoList);
    }

    @ResponseBody
    @RequestMapping("/house/getById")
    public ResponseData getById(String id) {
        House house = houseService.getById(id);
        return ResponseData.success(house);
    }

    /**
     * 获取当前登录用户的默认仓库
     * @return
     */
    @ResponseBody
    @RequestMapping("/house/getDefault")
    public ResponseData getDefault() {
        House house = houseService.getDefault(getLoginUserVo().getStoreId());
        return house==null ? ResponseData.fail("无默认仓库"): ResponseData.success(house);
    }
}
